package com.qzsoft.zhjd.common.components;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qzsoft.jeemis.common.page.PageData;
import com.qzsoft.jeemis.common.service.BaseService;

/**
 * @ClassName BaseServiceComponent
 * @Description 基础类工具类
 * @author linhuawei
 * @since 2021-09-16
 */
@Component
public class BaseServiceComponent extends BaseService {

    public <T> IPage<T> getPage(Map<String, Object> params, Class<T> resultType) {
        // 判断查询条件是否为空
        if (null == params) {
            IPage<T> page = new Page<T>();
            page.setPages(1L);
            page.setSize(10000L);
            return page;
        }

        // 获取分页数据
        coverPage(params);
        return super.getPage(params, resultType);
    }

    public <T> IPage<T> getPage(Map<String, Object> params, String defaultOrderField, boolean isAsc,
            Class<T> resultType) {
        // 判断查询条件是否为空
        if (null == params) {
            IPage<T> page = new Page<T>();
            page.setPages(1L);
            page.setSize(10000L);
            return page;
        }

        // 获取分页数据
        coverPage(params);
        return super.getPage(params, defaultOrderField, isAsc, resultType);
    }

    @SuppressWarnings("rawtypes")
    public <T> PageData<T> getPageData(IPage page, Class<T> target) {
        return super.getPageData(page, target);
    }

    public <T> PageData<T> getPageData(List<?> list, long total, Class<T> target) {
        return super.getPageData(list, total, target);
    }

    @SuppressWarnings("rawtypes")
    public QueryWrapper getQueryWrapper(Map<String, Object> mapParam) {
        if (null == mapParam) {
            return null;
        }
        return super.getQueryWrapper(mapParam);
    }

    public <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> mapParam, Class<T> target, boolean isStrict,
            List<String> likeFields, List<String> nullFields) {
        if (null == mapParam) {
            return null;
        }
        return super.getQueryWrapper(mapParam, target, isStrict, likeFields, nullFields);
    }

    public <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> mapParam, Class<T> target, boolean isStrict,
            String... likeFields) {
        if (null == mapParam) {
            return null;
        }
        return super.getQueryWrapper(mapParam, target, isStrict, likeFields);
    }

    public <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> mapParam, Class<T> target, String... likeFields) {
        if (null == mapParam) {
            return null;
        }
        return super.getQueryWrapper(mapParam, target, likeFields);
    }

    /**
     * @Title coverPage
     * @Description 因为前期系统分页参数跟框架不一致，添加一步参数转化
     * @param params
     */
    private void coverPage(Map<String, Object> params) {
        // 设置默认分页
        params.put("page", "1");
        params.put("limit", "10000");

        // 如果查询条件中有分页数据，提取分页数据
        if (params.get("current") != null) {
            params.put("page", params.get("current"));
        }
        if (params.get("size") != null) {
            params.put("limit", params.get("size"));
        }

        if (params.get("pageNo") != null) {
            params.put("page", params.get("pageNo"));
        }
        if (params.get("pageSize") != null) {
            params.put("limit", params.get("pageSize"));
        }
        if (params.get("pages") != null) {
            params.put("page", params.get("pages"));
        }
    }

}
